package com.algorithm;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 两个数组的交集
 */
public class IntersectionOfTwoArrays {

    public static int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> collect = Arrays.stream(nums2).boxed().collect(Collectors.toSet());
        return Arrays.stream(nums1).distinct().filter(collect::contains).toArray();

//        Set<Integer> set=new HashSet<>();
//        for (int i : nums1) {
//            if (collect.contains(i)){
//                set.add(i);
//            }
//        }
//        return set.toArray();
    }

    public static void main(String[] args) {
        int [] nums1={1,2,2,1};
        int [] nums2={2,2};
        int[] result = intersection(nums1, nums2);
        System.out.println(result);
    }
}
